#include "stdafx.h"
#include "CardDef.h"

struct  CardDefReflectionRegister
{
	CardDefReflectionRegister()
	{
		REGISTER_REFLECTION_MEMBER(CardTypeDef,m_iID,INVALID_ID).Editor_SetAsPrimaryKey();
		REGISTER_REFLECTION_MEMBER(CardTypeDef,m_sDisplayName,std::string("DefaultName")).Editor_SetAsDisplayName();
		REGISTER_REFLECTION_MEMBER(CardTypeDef,m_sName,std::string("GameName")).Editor_SetAsRaw();
		REGISTER_REFLECTION_MEMBER(CardTypeDef,m_iCost,0).Editor_SetAsRaw();
		REGISTER_REFLECTION_MEMBER(CardTypeDef,m_iCoolDown,4).Editor_SetAsRaw();
		REGISTER_REFLECTION_MEMBER(CardTypeDef,m_eRace,eCardRace_Human).Editor_SetAsEnumation<ECardRace>();
		REGISTER_REFLECTION_MEMBER(CardTypeDef,m_iIconID,0).Editor_SetAsRaw();
		REGISTER_REFLECTION_MEMBER(CardTypeDef,m_sCardFile,std::string("DefaultPic")).Editor_SetAsRaw();
		REGISTER_REFLECTION_MEMBER(CardTypeDef,m_iStar,0).Editor_SetAsRaw();
		REGISTER_REFLECTION_MEMBER(CardTypeDef,m_iSkill0,0).Editor_SetAsForeignKey(eDefGroup_Default,eDefaultDef_Reactors);
		REGISTER_REFLECTION_MEMBER(CardTypeDef,m_iSkill5,0).Editor_SetAsForeignKey(eDefGroup_Default,eDefaultDef_Reactors);
		REGISTER_REFLECTION_MEMBER(CardTypeDef,m_iSkill10,0).Editor_SetAsForeignKey(eDefGroup_Default,eDefaultDef_Reactors);
		REGISTER_REFLECTION_MEMBER(CardTypeDef,m_sDesc,std::string()).Editor_SetAsRaw();
		REGISTER_REFLECTION_MEMBER(CardTypeDef,m_vSource,std::vector<int32>()).Editor_SetAsForeignKeySet(eDefGroup_Default,eDefaultDef_DropSource);
		REGISTER_REFLECTION_MEMBER(CardTypeDef,m_iLibraryIndex,INVALID_ID).Editor_SetAsRaw();
		REGISTER_REFLECTION_MEMBER(CardTypeDef,m_eUsage,EUsage_Normal).Editor_SetAsEnumation<EUsage>();

		REGISTER_REFLECTION_MEMBER(CardDef,m_iID,INVALID_ID).Editor_SetAsPrimaryKey();
		REGISTER_REFLECTION_MEMBER(CardDef,m_sDisplayName,std::string("DefaultName")).Editor_SetAsDisplayName();
		REGISTER_REFLECTION_MEMBER(CardDef,m_iTypeID,INVALID_ID).Editor_SetAsForeignKey(eDefGroup_Default,eDefaultDef_CardTypes);
		REGISTER_REFLECTION_MEMBER(CardDef,m_iLvl,0).Editor_SetAsRaw();
		REGISTER_REFLECTION_MEMBER(CardDef,m_iAtk,0).Editor_SetAsRaw();
		REGISTER_REFLECTION_MEMBER(CardDef,m_iHp,0).Editor_SetAsRaw();
		REGISTER_REFLECTION_MEMBER(CardDef,m_iExpToNextLvl,0).Editor_SetAsRaw();
		REGISTER_REFLECTION_MEMBER(CardDef,m_iExpUsed,0).Editor_SetAsRaw();

		GlobalClassManager::Instance().RegisterClass(CardTypeDef::s_GetReflection(),eDefaultDef_CardTypes);
		GlobalDefManager::Instance().RegisterDefManger(&CardTypeDefManager::Instance());

		GlobalClassManager::Instance().RegisterClass(CardDef::s_GetReflection(),eDefaultDef_Card);
		GlobalDefManager::Instance().RegisterDefManger(&CardDefManager::Instance());
	}
}____CardDefReflectionRegister;

rewrite  void CardDef::OnArchieveReaded()
{
	m_pCardType=CardTypeDefManager::Instance().LookupItem(m_iTypeID);
	if(NULL==m_pCardType)
	{
		printf("CardDef::OnArchieveReaded NULL==m_pCardType,DefID=%d,TypeID=%d\n",m_iID,m_iTypeID);
	}
}



#define MAKE_CARD_TYPE_LEVEL_ID(type,level) (1000*type+level)
const CardDef * CardDefManager::LookupCardDefByLvl( int32 iType,int32 iLevel ) const
{
	std::map<int32,const CardDef *>::const_iterator it= m_mIndexs.find(MAKE_CARD_TYPE_LEVEL_ID(iType,iLevel));
	if(it==m_mIndexs.end()) return NULL;
	return it->second;
}

void CardDefManager::OnArchieveReaded()
{
	__CardDefManager::OnArchieveReaded();

	m_mIndexs.clear();
	std::vector<const CardDef*> vCardDefs;
	Enumerate(vCardDefs);
	for(uint32 ii=0;ii<vCardDefs.size();ii++)
	{
		m_mIndexs[MAKE_CARD_TYPE_LEVEL_ID(vCardDefs[ii]->m_iTypeID,vCardDefs[ii]->m_iLvl)]=vCardDefs[ii];
	}
}